{%- assign taboola_pixels = storeConfig.taboola | strtoarray -%}
{%- if taboola_pixels -%}
  <script type='text/javascript'>
    window._tfa = window._tfa || [];
    var taboola_pixels = {{taboola_pixels|json}};
    !function(t, f, a, x) {
      if (!document.getElementById(x)) {
        t.async = 1;
        t.src = a;
        t.id = x;
        f.parentNode.insertBefore(t, f);
      }
    }(document.createElement('script'), document.getElementsByTagName('script')[0], '//cdn.taboola.com/libtrc/unip/' + taboola_pixels[0].facebook_pixel_id + '/tfa.js', 'tb_tfa_script');
    function taboolaInit(name, obj) {
      for (var i = 0; i < taboola_pixels.length; i++) {
        window._tfa.push(Object.assign({
          notify: 'event',
          name: name,
          id: taboola_pixels[i].facebook_pixel_id,
          currency: "{{ base_currency.standard_code }}"
        }, obj));
      }
    };
        taboolaInit("page_view", {});
    {%- if template == 'product_detail' -%}
        taboolaInit("view_content", {});
    {%- elsif template == 'search_detail' and search.keywords -%}
        taboolaInit("search", {revenue:'{{ search.keywords | escape | append: ' '}}'.replace(/\s+$/g, "")});
    {%- elsif controller == 'order' and action == 'success' -%}
        taboolaInit("make_purchase", {
            {% if storeConfig.taboola_enable_ga %}
            revenue: "{{ order.current_subtotal_price| plus: order.current_coupon_price| plus: order.current_promotion_price }}",
            {% else %}
            revenue: "{{ order.total_price }}",
            {% endif %}
            orderid: '{{order.order_number}}'
        });
    {%- elsif template == 'blog_detail' -%}
        taboolaInit("page_view", {});
    {%- elsif template == 'collection_detail' -%}
        taboolaInit("page_view", {});
    {%- elsif template == 'page_detail' -%}
        taboolaInit("page_view", {});
    {%- endif -%};
    function callback_taboola_add_to_cart(ecdata) {
        taboolaInit("add_to_cart", {
          quantity: ecdata['ec_data_quantity'],
          revenue: ecdata['ec_data_price'] * ecdata['ec_data_quantity']
        });
    };
    function callback_taboola_generate_lead(ecdata) {};
    function callback_taboola_login(ecdata) {};
    function callback_taboola_begin_checkout(ecdata) {
        taboolaInit("start_checkout", {});
    };
    function callback_taboola_share_product(ecdata) {};
    function callback_taboola_remove_from_cart(ecdata) {};
    function callback_taboola_sign_up(ecdata) {
        taboolaInit("complete_registration", {});
    };
    function callback_taboola_add_address_information(ecdata) {};
    function callback_taboola_add_shipping_information(ecdata) {};
    function callback_taboola_add_payment_information(ecdata) {
        taboolaInit("add_payment_info", {});
    };
    function callback_taboola_to_wishlist(ecdata) {
        taboolaInit("add_to_wishlist", {});
    };
  </script>
{%- endif -%}